const http = require('http')


http.createServer((req, res) => {
  let url = req.url
  if (url === '/favicon.ico') {
    return res.end() // 忽略favicon.ico请求
  }
  console.log("请求路径", url)
  res.statusCode = 200
  res.setHeader('Content-Type', 'text/html;charset=UTF-8')
  res.end(renderHtml(url))
}).listen(3000, () => {
  console.log('Server running at http://localhost:3000/')
})

function renderHtml(url) {
  if (url === '/') {
    url = '首页'
  } else if (url === '/about') {
    url = '关于我们'
  } else if (url === '/contact') {
    url = '联系我们'
  } else {
    url = '未知页面'
  }
  return `
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Document</title>
    </head>
    <body>
        <h1>请求的URL是：${url}</h1>
    </body>
    </html>
  `
}